package srzy.dao

import java.sql.DriverManager
import srzy.common.Logging

abstract class DBUtil extends srzy.common.Logging {
  val url: String

  def executeUpdate(sqlList: List[String]) = {
    logger.debug("execute sql list start")
    val conn = DriverManager.getConnection(url)
    try {
      val statement = conn.createStatement
      sqlList.map((sql: String) => {
        logger.debug("sql:" + sql)
        statement.executeUpdate(sql)
      })
      conn.commit
    } finally {
      conn.close
    }
    logger.debug("executeSQL over")
  }

  def executeUpdate(sql: String) = {
    logger.debug("execute sql start")
    val conn = DriverManager.getConnection(url)
    try {
      val statement = conn.createStatement
      logger.debug("sql:" + sql)
      statement.executeUpdate(sql)
      conn.commit
    } finally {
      conn.close
    }
    logger.debug("executeSQL over")
  }

  def executeLine(sql: String): String = {
    logger.debug("executeLine start")
    val conn = DriverManager.getConnection(url)

    try {
      val statement = conn.createStatement
      logger.debug("sql:" + sql)
      val rs = statement.executeQuery(sql)
      if (!rs.next()) {
        return ""
      }
      return rs.getString(1)
      //      res = rs.getString(1)
      //      return res

    } finally {
      conn.close
    }
  }
}